#!/bin/bash
#****************************************************************#
# ScriptName: clear.sh
# Function: clear sysom
#***************************************************************#
APP_HOME=/usr/local/sysom

if [ $# -lt 1 ] ; then
    echo "USAGE: $0 INSTALL_DIR"
    echo "Or we use default install dir: /usr/local/sysom/"
    echo "E.g.: $0 /usr/local/sysom"
else
    APP_HOME=$1
fi

export APP_HOME=${APP_HOME}
export SERVER_HOME=${APP_HOME}/server

del_app_home() {
    # Whether delete APP_HOME
    if [ -d "${APP_HOME}" ] && [ "`ls -A ${APP_HOME}`" = "script" ]; then
        rm -r ${APP_HOME}
    fi
}

del_conf() {
    if [ -f "/etc/nginx/nginx.conf.bak" ];then
        mv /etc/nginx/nginx.conf.bak /etc/nginx/nginx.conf
    fi
    if [ -f "/etc/nginx/conf.d/sysom.conf" ];then
        rm -f /etc/nginx/conf.d/sysom.conf
    fi
    if [ -f "/etc/supervisord.d/sysom.ini" ];then
        rm -f /etc/supervisord.d/sysom.ini
    fi
    if [ -f "/usr/lib/systemd/system/sysom-server.service" ];then
        rm -f /usr/lib/systemd/system/sysom-server.service
    fi
}

BaseDir=$(dirname $(readlink -f "$0"))
clear() {
    bash -x ${APP_HOME}/init_scripts/server/clear.sh
    del_conf
    del_app_home
}

clear
